// index.js
Page({
	data:{
		takingFlag:false,
		chosingFlag:false
	},
	agreeUsingtaking:function () {
		const takingFlag  = this.data.takingFlag
		console.log(takingFlag)
		if (!takingFlag) {
			return ;
		}
		wx.chooseMedia({
			count: 1,
			sizeType: ['original'],
			sourceType: ['camera'],
			mediaType: ['image'],
			camera: ['back'],
			success: function (res) {
				const tempFilePaths = res.tempFiles[0].tempFilePath;
				wx.setStorageSync('takingPhoto', res.tempFiles[0].tempFilePath);	
				console.log(res.tempFiles[0].tempFilePath);
				// 图像处理
			},
			fail: function (error) {
				console.error(error);
				// 图像处理
			}
		});
	},
	takePhoto: function () {
		let that = this
		wx.getSetting({
    success(res) {
        if (!res.authSetting['scope.camera']) {
            wx.authorize({
                scope: 'scope.camera',
                success() {
                    console.log('授权成功');
										that.setData({
											takingFlag:true
										})
										that.agreeUsingtaking()
                },
                fail() {
                    wx.showModal({
                        title: '授权提示',
                        content: '您需要授权相机权限才能使用该功能。',
                        showCancel: true,
                        cancelText: '取消',
                        confirmText: '设置',
                        success(res) {
                            if (res.confirm) {
                                wx.openSetting({
                                    success(settingRes) {
                                        if (settingRes.authSetting['scope.camera']) {
                                            console.log('用户重新授权成功');
																						that.setData({
						                                 	takingFlag:true
			                                			})
                                        } else {
                                            console.log('用户未授权');
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            });
        } else {
				    that.setData({
							takingFlag:true
						})
            console.log('已经授权1');
						that.agreeUsingtaking()
            // 例如调用相机拍照功能
						// this.that.agreeUsingtaking()
						// console.log(taking);
        }
    },
    fail() {
        console.log('获取设置失败');
        wx.showToast({
            title: '获取设置失败',
            icon: 'none'
        });
    },
    });
  },
	agreeUsingchosing:function () {
		const chosingFlag  = this.data.chosingFlag
		console.log(chosingFlag)
		if (!chosingFlag) {
			return ;
		}
		wx.chooseMedia({
			count: 1,
			sizeType: ['original'],
			sourceType: ['album'],
			mediaType: ['image'],
			success: function (res) {
				const tempFilePaths = res.tempFiles[0].tempFilePath;
				wx.setStorageSync('takingPhoto', res.tempFiles[0].tempFilePath);	
				// 图像处理
			},
			fail: function (error) {
				console.error(error);
				// 图像处理
			}
		});
	},
	chooseImage: function () {
    let that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              console.log('相册访问授权成功');
              that.setData({
								chosingFlag:true
							})
							that.agreeUsingchosing()
            },
            fail() {
              wx.showModal({
                title: '授权提示',
                content: '您需要授权相册权限才能选择图片。',
                showCancel: true,
                cancelText: '取消',
                confirmText: '设置',
                success(res) {
                  if (res.confirm) {
                    wx.openSetting({
                      success(settingRes) {
                        if (settingRes.authSetting['scope.writePhotosAlbum']) {
                          console.log('用户重新授权成功');
                          that.setData({
								            chosingFlag:true
							            })
													// that.agreeUsingchosing();
                        } else {
                          console.log('用户未授权');
                        }
                      }
                    });
                  }
                }
              });
            }
          });
        } else {
					that.setData({
								chosingFlag:true
							})
          that.agreeUsingchosing();
        }
      },
      fail() {
        console.log('获取设置失败');
        wx.showToast({
          title: '获取设置失败',
          icon: 'none'
        });
      },
    });
},
	navigateToUsing: function () {
		wx.navigateTo({
			url: '/pages/usingWarning/usingWarning',
			success: function (res) {
				console.log("成功跳转到使用须知页面");
			},
			fail: function (err) {
				console.error("跳转到处理使用须知失败", err);
			}
		});
	},
	navigateToKnowledge: function () {
		wx.navigateTo({
			url: '/pages/knowledge/knowledge',
			success: function (res) {
				console.log("成功跳转到菌种科普页面");
			},
			fail: function (err) {
				console.error("跳转到菌种科普失败", err);
			}
		});
	},
	navigateToHandling: function () {
		wx.navigateTo({
			url: '/pages/handling/handling',
			success: function (res) {
				console.log("成功跳转到处理记录页面");
			},
			fail: function (err) {
				console.error("跳转到处理记录页面失败", err);
			}
		});
	},
	navigateTosaving: function () {
		wx.navigateTo({
			url: '/pages/saving/saving',
			success: function (res) {
				console.log("成功跳转到保存的记录页面");
			},
			fail: function (err) {
				console.error("跳转到保存的记录页面失败", err);
			}
		});
	},
	open: function () {
		wx.login({
      success: function(res) {
       console.log(res);
        if (res.code) {
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      },
  fail: function(){
    console.log("启用wx.login函数，失败！");
  },
  complete:function(){
    console.log("已启用wx.login函数");
  }
});
	},
	onShow() {
		if (typeof this.getTabBar === 'function' && this.getTabBar()) {
			this.getTabBar().setData({
				selected: 0
			})
		}
	}
})